﻿using System;
using System.Collections;
using System.IO;
using System.Xml;
namespace Zgq.CommonTools
{
    public static class XmlUtility
    {
        public static string GetXmlText(string fileName, string nodeName)
        {
            //string fileName;

            //string currentDirectory = System.Windows.Forms.Application.StartupPath;
            //fileName = string.Format(@"{0}\\{1}", currentDirectory, fileName);

            XmlDocument _document;
            if (System.IO.File.Exists(fileName))
            {
                _document = new XmlDocument();
                _document.Load(fileName);
                //如果只读时把只读去掉
                if (System.IO.File.GetAttributes(fileName) != FileAttributes.Normal)
                {
                    System.IO.File.SetAttributes(fileName, FileAttributes.Normal);
                }
            }
            else
            {
                return null;
            }

            XmlNode nodeSOLUTION = _document.SelectSingleNode("SOLUTION");
            if (nodeSOLUTION != null)
            {
                var languageNode = _document.DocumentElement.SelectSingleNode(nodeName);

                if (languageNode == null)
                    return "";
                else if (languageNode.Attributes["value"] == null)
                    return "";  //value不存在时要重写
                else
                    return languageNode.Attributes["value"].InnerText;  //返回数据
            }
            return null;
        }

        public static void SetXmlText(string fileName, string nodeName, string Value)
        {
            XmlDocument _document = new XmlDocument();
            if (System.IO.File.Exists(fileName))
            {
                _document.Load(fileName);
                //如果只读时把只读去掉
                if (System.IO.File.GetAttributes(fileName) != FileAttributes.Normal)
                {
                    System.IO.File.SetAttributes(fileName, FileAttributes.Normal);
                }
                XmlNode nodeSOLUTION = _document.SelectSingleNode("SOLUTION");
                if (nodeSOLUTION != null)
                {
                    var languageNode = _document.DocumentElement.SelectSingleNode(string.Format("{0}", nodeName));
                    if (languageNode != null)
                    {
                        var attri = languageNode.Attributes["value"];
                        if (attri == null)
                        {
                            XmlDocument doc = languageNode.OwnerDocument;
                            XmlAttribute attr = null;
                            attr = doc.CreateAttribute("value");
                            attr.Value = Value;
                            languageNode.Attributes.SetNamedItem(attr);

                            languageNode.Attributes.Append(attr);

                        }
                        else
                            attri.InnerText = Value;  //返回数据
                    }
                    else
                    {
                        XmlElement node = _document.CreateElement(nodeName);
                        node.SetAttribute("value", Value);
                        nodeSOLUTION.AppendChild(node);
                    }
                }
                else
                {
                    //创建一个根节点（一级）
                    XmlElement root = _document.CreateElement("SOLUTION");
                    _document.AppendChild(root);
                    //创建节点（二级）
                    XmlElement node = _document.CreateElement(nodeName);
                    node.SetAttribute("value", Value);
                    root.AppendChild(node);

                    _document.Save(fileName);
                }
                _document.Save(fileName);
            }
            else
            {
                //创建一个根节点（一级）
                XmlElement root = _document.CreateElement("SOLUTION");
                _document.AppendChild(root);
                //创建节点（二级）
                XmlElement node = _document.CreateElement(nodeName);
                node.SetAttribute("value", Value);
                root.AppendChild(node);

                _document.Save(fileName);
            }
        }

        public static void SetSvcXmlText(string fileName, string firstNodeStr, string nodeNameStr, string Value)
        {
            XmlDocument _document = new XmlDocument();
            if (System.IO.File.Exists(fileName))
            {
                _document.Load(fileName);
                //如果只读时把只读去掉
                if (System.IO.File.GetAttributes(fileName) != FileAttributes.Normal)
                {
                    System.IO.File.SetAttributes(fileName, FileAttributes.Normal);
                }
                XmlNode nodeSOLUTION = _document.SelectSingleNode("SOLUTION");
                if (nodeSOLUTION != null)
                {
                    XmlNode firstNode = nodeSOLUTION.SelectSingleNode(firstNodeStr);//（服务类型）
                    if (firstNode != null)
                    {
                        var languageNode = firstNode.SelectSingleNode(nodeNameStr);//（服务参数）
                        if (languageNode != null)
                        {
                            languageNode.InnerText = Value;  //返回数据
                        }
                        else
                        {
                            XmlElement node = _document.CreateElement(nodeNameStr);
                            node.InnerText = Value;  //返回数据
                            firstNode.AppendChild(node);
                        }
                    }
                    else
                    {
                        firstNode = _document.CreateElement(firstNodeStr);
                        nodeSOLUTION.AppendChild(firstNode);

                        XmlElement node = _document.CreateElement(nodeNameStr);
                        node.InnerText = Value;  //返回数据
                        firstNode.AppendChild(node);
                    }

                }
                else
                {
                    //创建一个根节点（一级）
                    XmlElement root = _document.CreateElement("SOLUTION");
                    _document.AppendChild(root);
                    //创建节点（二级）
                    XmlElement firstNode = _document.CreateElement(firstNodeStr);
                    root.AppendChild(firstNode);

                    XmlElement node = _document.CreateElement(nodeNameStr);
                    node.InnerText = Value;  //返回数据
                    firstNode.AppendChild(node);
                }
                _document.Save(fileName);
            }
            else
            {
                //创建一个根节点（一级）
                XmlElement root = _document.CreateElement("SOLUTION");
                _document.AppendChild(root);
                //创建节点（二级）
                XmlElement node = _document.CreateElement(nodeNameStr);
                node.SetAttribute("value", Value);
                root.AppendChild(node);

                _document.Save(fileName);
            }
        }

        public static string GetSvcXmlText(string fileName, string firstNodeStr, string nodeNameStr)
        {
            XmlDocument _document;
            if (System.IO.File.Exists(fileName))
            {
                _document = new XmlDocument();
                _document.Load(fileName);
                //如果只读时把只读去掉
                if (System.IO.File.GetAttributes(fileName) != FileAttributes.Normal)
                {
                    System.IO.File.SetAttributes(fileName, FileAttributes.Normal);
                }
            }
            else
            {
                return "";
            }
            string result = "";
            XmlNode nodeSOLUTION = _document.SelectSingleNode("SOLUTION");
            if (nodeSOLUTION != null)
            {
                var firstNode = nodeSOLUTION.SelectSingleNode(firstNodeStr);

                if (firstNode != null)
                {
                    var nodeName = firstNode.SelectSingleNode(nodeNameStr);
                    if (nodeName != null)
                    {
                        result = nodeName.InnerText;
                    }
                }
            }
            return result;
        }

        public static string GetVal(string xmlFile, string xmlPath, string attribute)
        {
            string valStr = null;

            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFile);
            //使用xPath选择需要的节点
            XmlNode nd = doc.SelectSingleNode(xmlPath);

            if (nd != null)
            {
                if (string.IsNullOrEmpty(attribute))
                    valStr = nd.InnerText;
                else
                    valStr = nd.Attributes[attribute].InnerText;
            }

            return valStr;
        }
    }
}